package com.shigu.constant.action;

import com.opentae.data.mall.beans.ManagerPageConstant;
import com.shigu.MallResponse;
import com.shigu.PageResponse;
import com.shigu.constant.bo.AddConstantsRequest;
import com.shigu.constant.bo.ModifyConstantsRequest;
import com.shigu.constant.bo.MyconstantsRequest;
import com.shigu.constant.service.AdminConstantService;
import com.shigu.constant.service.SystemConstantService;
import com.shigu.constant.vo.ConstantResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;

/**
 * @author Mr.赵
 * created on 2018/11/7
 */
@Controller
@RequestMapping("/adminjson")
public class AdminSystemController {

    @Autowired
    AdminConstantService adminConstantService;
    @Autowired
    SystemConstantService systemConstantService;

    /**
     * 添加常量
     * @return
     */
    @RequestMapping("addConstants")
    @ResponseBody
    public MallResponse addConstants(@RequestBody AddConstantsRequest bo){
        adminConstantService.addConstant(bo);
        return new MallResponse().success();
    }

    @RequestMapping("delConstants")
    @ResponseBody
    public MallResponse delConstants(Long constantId){
        adminConstantService.delConstant(constantId);
        return new MallResponse().success();
    }

    @RequestMapping("modifyConstants")
    @ResponseBody
    public MallResponse modifyConstants(@RequestBody ModifyConstantsRequest bo){
        adminConstantService.modifyConstant(bo);
        return new MallResponse().success();
    }

    @RequestMapping("myconstants")
    @ResponseBody
    public PageResponse<ManagerPageConstant> myconstants(MyconstantsRequest bo){
        return adminConstantService.selList(bo);
    }

    @RequestMapping("synConstants")
    @ResponseBody
    public MallResponse synConstants(){
        systemConstantService.loadConstantValue();
        return new MallResponse().success();
    }

    /**
     * 获得常量
     * @param uri
     * @return
     */
    @RequestMapping("constant")
    @ResponseBody
    public ConstantResponse constant(String uri){
        ConstantResponse res=new ConstantResponse();
        res.setConstant(systemConstantService.selPageConstant(uri));
        if (res.getConstant() == null) {
            res.setConstant(new HashMap<>());
        }
        return res.success();
    }
}
